package com.baselib.libnetwork.net.libnetworkcomponent;

import com.baselib.libnetwork.net.callback.JsonCallback;
import com.baselib.libnetwork.net.model.ResultResponse;

/**
 * @author lixuce
 * @version v1.0
 * @description
 * @since 2018/1/23
 */

public abstract class HttpCallback<T> extends JsonCallback<T> {
    public abstract void onSuccess(T t);


    @Deprecated
    @Override
    public void onSuccess(ResultResponse<T> response) {
        T body = response.body();
        if (body instanceof String) {
            onSuccess(body);
        } else {
            BaseResponse<T> baseResponse = (BaseResponse<T>) body;
            if(baseResponse == null) {
                OnFailureNeedData(-1, body);
                return;
            }
            int code = baseResponse.code;
            if (code == 0) {
                onSuccess(body);
            } else {
                OnFailureNeedData(code, body);
            }
        }
    }

    /**
     * 服务器返回非正确code码，同时包含数据
     * @param code
     * @param t
     */
    protected abstract void OnFailureNeedData(int code, T t);

}
